var User = require('../model').User;
// var logger = require('../logger');

var mod = {
    login: function(userInfo, fn) {
        User.find({userName: userInfo.userName, password: userInfo.password}).
        select(['userName', 'role']).
        exec(fn);
    },

    getUser: function(option, fn) {
        console.log('option---', User.find({}));
        User.find(option).
        select(['userName', 'role']).
        exec(fn);
    },

    delete: function(userId, fn) {
        User.remove({_id: userId}).exec(fn);
    },

    addUser: function(option, fn) {
        var user = new User({
            userName: option.userName,
            password: option.password,
            role: option.role
        });
        user.save(fn);
    },

    // 查询非好友列表
    getUnFriend(userId, fn) {
        User.findOne({_id: userId}, {friends: 1}, function(err, data) {
            
                var arr = [];

                arr.push(data._id);
                for(var i = 0; i < data.friends.length; i++) {
                    arr.push(data.friends[i].friendId);
                }
                User.find({_id: {$nin: arr}}, {userName: 1, role: 1}).exec(fn);
                // fn(err, arr);
        });
    },

    // 获取好友列表 getFriendList
    getFriendList: function(userId, fn) {
        User.findOne({_id: userId}, {friends: 1}, function(err, data) {
            
            var arr = [];

            for(var i = 0; i < data.friends.length; i++) {
                arr.push(data.friends[i].friendId);
            }
            User.find({_id: {$in: arr}}, {userName: 1, role: 1}).exec(fn);
        });
    },

    // 添加好友
    addFriend: function(option, fn) {
        
        User.update({_id: option.userId}, {
            $push: {
                friends: {
                    friendId: option.friendId, 
                    remarks: option.remarks
                }
            }
        }, function(err, result) {
            if(fn) {
                fn(err, result);
            }
        });
    },

    register: function() {
        var user = new User({
            userName: 'zhangsan',
            password: '123456'
        });
        user.save();

    }

}

module.exports = mod;